package com.lottery.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

@Slf4j
@RestController
@RequestMapping("apk")
public class ApkController {
    //        private static final String APK_FILE_PATH = "D:\\adding\\lottery\\apk\\";
    private static final String APK_FILE_PATH = File.separator + "opt" + File.separator + "softwares" + File.separator + "apk" + File.separator;


    /*** 提供文件供客户端下载 ***/
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        log.info("开始下载apk...");
        File file = null;

        File dir = new File(APK_FILE_PATH);
        for (File f : dir.listFiles()) {
            if (f.exists() && f.isFile()) {
                file = f;
            }
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment", file.getName());
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentLength(file.length());

        return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
    }

    @RequestMapping("/upload")
    public void upload(HttpServletRequest request) throws IOException {
        log.info("开始上传apk...");
        //删除目录下的apk文件
        File dir = new File(APK_FILE_PATH);
        for (File file : dir.listFiles()) {
            file.delete();
        }


        //创建一个通用的多部分解析器
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //判断 request 是否有文件上传,即多部分请求
        if (multipartResolver.isMultipart(request)) {
            //转换成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            //取得request中的所有文件名
            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                //记录上传过程起始时的时间，用来计算上传时间
                int pre = (int) System.currentTimeMillis();
                //取得上传文件
                MultipartFile file = multiRequest.getFile(iter.next());
                if (file != null) {
                    //取得当前上传文件的文件名称
                    String myFileName = file.getOriginalFilename();
                    //如果名称不"",说明该文件存在，否则说明该文件不存在
                    if (!myFileName.trim().equals("")) {
                        System.out.println(myFileName);
                        //重命名上传后的文件名
                        String fileName = "f-" + file.getOriginalFilename();
                        //定义上传路径
                        String path = APK_FILE_PATH + fileName;
                        File localFile = new File(path);
                        file.transferTo(localFile);
                    }
                }
                //记录上传该文件后的时间
                int finaltime = (int) System.currentTimeMillis();
                System.out.println(finaltime - pre);
            }
        }
    }

}
